from typing import *


class Solution:
    def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
        nums.sort()
        ans = []
        for i in range(0, len(nums) - 2, 3):
            if nums[i + 2] - nums[i] <= k:
                cur = []
                for j in range(3):
                    cur.append(nums[i + j])
                ans.append(cur)
            else:
                return []
        return ans


s = Solution()
print(s.divideArray(nums=[1, 3, 4, 8, 7, 9, 3, 5, 1], k=2))
